<?php

namespace app\admin\model\courses;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'courses_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'paytime_text',
        'orderstate_text',
        'paystate_text'
    ];
    

    
    public function getOrderstateList()
    {
        return ['0' => __('Orderstate 0'), '1' => __('Orderstate 1'), '2' => __('Orderstate 2'), '3' => __('Orderstate 3'), '4' => __('Orderstate 4')];
    }

    public function getPaystateList()
    {
        return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderstateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['orderstate']) ? $data['orderstate'] : '');
        $list = $this->getOrderstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaystateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paystate']) ? $data['paystate'] : '');
        $list = $this->getPaystateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function courses()
    {
        return $this->belongsTo('Courses', 'courses_courses_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function log()
    {
        return $this->belongsTo('app\admin\model\admin\Log', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
